package com.stylefeng.guns.modular.system.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.system.model.EmailUser;
import com.stylefeng.guns.modular.system.service.IEmailUserService;
import com.stylefeng.guns.modular.system.warpper.EmailUserWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 邮件用户关系管理控制器
 *
 * @author fengshuonan
 * @Date 2018-05-18 23:17:11
 */
@Controller
@RequestMapping("/emailUser")
public class EmailUserController extends BaseController {

    private String PREFIX = "/system/emailUser/";

    @Autowired
    private IEmailUserService emailUserService;

    /**
     * 跳转到邮件用户关系管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "emailUser.html";
    }

    /**
     * 跳转到添加邮件用户关系管理
     */
    @RequestMapping("/emailUser_add")
    public String emailUserAdd() {
        return PREFIX + "emailUser_add.html";
    }

    /**
     * 跳转到修改邮件用户关系管理
     */
    @RequestMapping("/emailUser_update/{emailUserId}")
    public String emailUserUpdate(@PathVariable Integer emailUserId, Model model) {
        EmailUser emailUser = emailUserService.selectById(emailUserId);
        model.addAttribute("item",emailUser);
        LogObjectHolder.me().set(emailUser);
        return PREFIX + "emailUser_edit.html";
    }

    /**
     * 获取邮件用户关系管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String condition) {
        List<Map<String, Object>> eu = emailUserService.selectLikeCondition(condition);

        return new EmailUserWarpper(eu).warp();
    }

    /**
     * 新增邮件用户关系管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(EmailUser emailUser) {
        emailUserService.insert(emailUser);
        return SUCCESS_TIP;
    }

    /**
     * 删除邮件用户关系管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer emailUserId) {
        emailUserService.deleteById(emailUserId);
        return SUCCESS_TIP;
    }

    /**
     * 修改邮件用户关系管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(EmailUser emailUser) {
        emailUserService.updateById(emailUser);
        return SUCCESS_TIP;
    }

    /**
     * 邮件用户关系管理详情
     */
    @RequestMapping(value = "/detail/{emailUserId}")
    @ResponseBody
    public Object detail(@PathVariable("emailUserId") Integer emailUserId) {
        return emailUserService.selectById(emailUserId);
    }
}
